package za.co.yellowfire.solarflare.jpa;

import za.co.yellowfire.solarflare.SearchManagerBean;

import javax.persistence.PostPersist;
import javax.persistence.PostRemove;
import javax.persistence.PostUpdate;

/**
 * An entity listener that persists changes to the search index
 * when an JPA entity is persisted or removed.
 *
 * @author Mark P Ashworth
 * @version 0.1
 */
public class SearchIndexListener {

    private SearchManagerBean search = new SearchManagerBean();

    /**
     * Triggered after a JPA entity is persisted or updated
     * @param entity The entity to index
     */
    @PostPersist
    @PostUpdate
    public void postPersist(Object entity) {
        search.onSearchableAdded(entity);
    }

    /**
     * Triggered after a JPA entity is deleted
     * @param entity The entity to remove from the index
     */
    @PostRemove
    public void postRemove(Object entity) {
        search.onSearchableRemoved(entity);
    }
}
